package groupd

import (
	"time"

	cgroupd "gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/groupd"

	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/common"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

// rule 组规则定义
type rule struct {
	ColumnID string   `bson:"columnid,omitempty"` // 栏目
	Org      string   `bson:"org,omitempty"`      // 组织
	Own      string   `bson:"own,omitempty"`      // 所有者
	Key      string   `bson:"key,omitempty"`      // 关键字		针对信息内的文本内容做全文检索
	Tags     []string `bson:"tags,omitempty"`     // 标签		完全匹配所有标签
}

func (r rule) To() cgroupd.Rule {
	return cgroupd.Rule{
		ColumnID: r.ColumnID,
		Org:      r.Org,
		Own:      r.Own,
		Key:      r.Key,
		Tags:     r.Tags,
	}
}
func fullRule(r cgroupd.Rule) rule {
	return rule{
		ColumnID: r.ColumnID,
		Org:      r.Org,
		Own:      r.Own,
		Key:      r.Key,
		Tags:     r.Tags,
	}
}

type rules []rule

func (r rules) To() []cgroupd.Rule {
	l := len(r)
	if l > 0 {
		tmp := make([]cgroupd.Rule, l)
		for i, v := range r {
			tmp[i] = v.To()
		}
		return tmp
	}
	return nil
}
func fullRules(cr []cgroupd.Rule) rules {
	l := len(cr)
	if l > 0 {
		tmp := make(rules, l)
		for i, v := range cr {
			tmp[i] = fullRule(v)
		}
		return tmp
	}
	return nil
}

// @collname 信息组集合(i_group)
type collGroup struct {
	GroupID    string            `bson:"_id" `
	Owner      string            `bson:"owner"`                //所属用户
	Org        string            `bson:"org"`                  //所属组织
	Kind       cgroupd.GroupKind `bson:"kind"`                 //类型（0,手动，1自动）
	Name       string            `bson:"name"`                 //组名称
	ShortName  string            `bson:"shortName"`            //简称
	Icon       []string          `bson:"icon"`                 //图标
	Desc       string            `bson:"desc"`                 //简介
	InfoWeight int               `bson:"infoWeight"`           //权重模式 0.保持收录信息的权重 ，1.默认信息的权重0，
	Rules      rules             `bson:"rules"`                //组规则
	Flags      []string          `bson:"flags"`                //用户自行定义组标识
	Status     cgroupd.GStatus   `bson:"status"`               //状态 状态 -1 删除  0 停止  1 收集
	InfoNum    int               `bson:"infoNum"`              //信息总数量
	Creator    string            `bson:"creator"`              //创建人
	CreTime    time.Time         `bson:"creTime"`              //创建时间
	UpdateTime time.Time         `bson:"updateTime"`           //最后更新时间
	Updater    string            `bson:"updater"`              //最后更新人
	DeleteTime time.Time         `bson:"deleteTime,omitempty"` //删除时间
	Deleter    string            `bson:"deleter,omitempty"`    //删除人

}

func (c collGroup) CollID() string {
	return c.GroupID
}
func (collGroup) CollName() string {
	return collNameGroup
}
func (c collGroup) IsCache() bool {
	return c.GroupID != "" && c.Status == cgroupd.GSNormal
}
func (c *collGroup) FullDesc(e cgroupd.GroupDesc) {
	c.Owner = e.Owner
	c.Org = e.Org
	c.Kind = e.Kind
	c.Name = e.Name
	c.ShortName = e.ShortName
	c.Icon = e.Icon
	c.Desc = e.Desc

}

func (c collGroup) ToDesc() cgroupd.GroupDesc {
	return cgroupd.GroupDesc{
		Owner:     c.Owner,
		Org:       c.Org,
		Kind:      c.Kind,
		Name:      c.Name,
		ShortName: c.ShortName,
		Icon:      c.Icon,
		Desc:      c.Desc,
	}
}
func (c *collGroup) FullCtlParam(e cgroupd.GroupCtlParam) {

	c.InfoWeight = e.InfoWeight
	c.Rules = fullRules(e.Rules)
	c.Flags = e.Flags

}

func (c collGroup) ToCtlParam() cgroupd.GroupCtlParam {
	return cgroupd.GroupCtlParam{
		InfoWeight: c.InfoWeight,
		Rules:      c.Rules.To(),
		Flags:      c.Flags,
	}
}

func (c *collGroup) FullStatus(e cgroupd.GroupStatus) {
	c.Status = e.Status
	c.InfoNum = e.InfoNum

}
func (c collGroup) ToStatus() cgroupd.GroupStatus {
	return cgroupd.GroupStatus{
		Status:  c.Status,
		InfoNum: c.InfoNum,
	}
}

func (c *collGroup) FullOperator(e common.Operator) {
	c.Creator = e.Creator
	c.CreTime = e.CreTime
	c.UpdateTime = e.UpdateTime
	c.Updater = e.Updater
	c.DeleteTime = e.DeleteTime
	c.Deleter = e.Deleter

}
func (c collGroup) ToOperator() common.Operator {
	return common.Operator{
		Creator:    c.Creator,
		CreTime:    c.CreTime,
		UpdateTime: c.UpdateTime,
		Updater:    c.Updater,
		DeleteTime: c.DeleteTime,
		Deleter:    c.Deleter,
	}
}

func (c collGroup) ToAggregate() ddd.Aggregate {
	return c.ToGroupAggregate()
}
func (c collGroup) ToGroupAggregate() *cgroupd.GroupAggregate {
	return &cgroupd.GroupAggregate{
		GroupID:       c.GroupID,
		GroupDesc:     c.ToDesc(),
		GroupStatus:   c.ToStatus(),
		Operator:      c.ToOperator(),
		GroupCtlParam: c.ToCtlParam(),
	}
}

// @collname 信息组内信息集合(i_groupInfo)
type collGroupInfo struct {
	GIID        string    `bson:"_id,omitempty"` // 编码
	GroupID     string    `bson:"groupId"`       // 信息组编码
	InfoID      string    `bson:"infoId"`        // 信息编码
	Weight      int       `bson:"weight"`        // 权重
	Seq         string    `bson:"seq"`           // 内部排序号
	CreTime     time.Time `bson:"creTime"`
	Title       string    `bson:"title"`       // 标题
	ColumnID    string    `bson:"column_id"`   // 栏目编号
	ColumnName  string    `bson:"column_name"` // 栏目名称
	PublishTime time.Time `bson:"pub_time"`    // 发布时间
}

func (c collGroupInfo) CollID() string {
	return c.GIID
}
func (collGroupInfo) CollName() string {
	return collNameGroupInfo
}
